package fr.uvsq.isty.bdh.hchess.gui.menu;

import fr.uvsq.isty.bdh.hchess.gui.game.BoardDisplay;
import fr.uvsq.isty.bdh.hchess.gui.MainWindow;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 * Online Menu is the menu (JPanel) allowing the user to enter his login, password and an address (+port).
 * @author Nathan
 *
 */
public class OnlineMenu extends JPanel{
	
	//Attributes
	
	/**
	 * Background image
	 */
	private Image bg;
	
	/**
	 * Parent window
	 */
	private MainWindow MW;
	
	/**
	 * Bottom panel, where the back and start button are.
	 */
	private JEmptyPanel bottom;	
	/**
	 * The button to start
	 */
	private JButton start;
	/**
	 * The button to go back to the menu.
	 */
	private JButton back;
	
	
	private JEmptyPanel center;

	private JLabel addressLab;
	private JTextArea addressIn;
	
	private JLabel portLab;
	private JTextArea portIn;
	
	private JLabel loginLab;
	private JTextArea loginIn;
	
	private JLabel passwordLab;
	private JTextArea passwordIn;
	
	
	//methods
	
	/**
	 * Constructor, where everything is done.
	 * @param parent the parent window containing this Panel. To be able to change the content.
	 */
	public OnlineMenu(MainWindow parent)
	{
		this.MW = parent;
		this.setPreferredSize(new Dimension(BoardDisplay.DEFAULT_WIDTH, BoardDisplay.DEFAULT_HEIGHT));
		
		this.setLayout(new BorderLayout());
		
		//Background
		try {
			bg = ImageIO.read(getClass().getClassLoader()
                                .getResource("menu/menu.jpg"));
		} catch (IOException e1) {			
			e1.printStackTrace();
		}
		
		
		//Bottom buttons (back & start)
		bottom = new JEmptyPanel();
		
		//Button Back
		back = new JButton("Retour");		
			
		bottom.add(back, BorderLayout.PAGE_START);
		
		back.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	MW.setContentPane(new MainMenu(MW));
            	MW.setVisible(true);
            	MW.repaint();
            }
        });
		
		//Button Start
		start = new JButton("Lancer la partie");	
		
		bottom.add(start, BorderLayout.PAGE_END);
		
		start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               
            }
        });		
		
		//Bottom area
		this.add(bottom, BorderLayout.PAGE_END);
		
		
		//Left area
		
		this.add(new JEmptyPanel(), BorderLayout.WEST);
		
		//Right area
		
		this.add(new JEmptyPanel(), BorderLayout.EAST);
		
		//top area
		JEmptyPanel top = new JEmptyPanel();
		top.setPreferredSize(new Dimension(100, 150) );
		
		this.add(top, BorderLayout.NORTH);
	
		
		
		///center area
		center = new JEmptyPanel();
		//We have to put a panel inside another one, in the other case, it won"t work.
		JEmptyPanel centerCenterP = new JEmptyPanel();
		
		
		//address
		addressLab = new JLabel("Adresse du serveur");
		addressIn = new JTextArea();		
		
		addressLab.setAlignmentX(Component.CENTER_ALIGNMENT);
		centerCenterP.setLayout(new BoxLayout(centerCenterP, BoxLayout.PAGE_AXIS));	
		centerCenterP.add(Box.createVerticalGlue()); // upper space
		centerCenterP.add(addressLab);
		
		addressIn.setAlignmentX(Component.CENTER_ALIGNMENT);
		centerCenterP.setLayout(new BoxLayout(centerCenterP, BoxLayout.PAGE_AXIS));			
		centerCenterP.add(addressIn);
		centerCenterP.add(Box.createVerticalGlue());
		
		//port
		portLab = new JLabel("Port");
		portIn = new JTextArea();		
		
		portLab.setAlignmentX(Component.CENTER_ALIGNMENT);
		centerCenterP.setLayout(new BoxLayout(centerCenterP, BoxLayout.PAGE_AXIS));			
		centerCenterP.add(portLab);
		
		portIn.setAlignmentX(Component.CENTER_ALIGNMENT);
		centerCenterP.setLayout(new BoxLayout(centerCenterP, BoxLayout.PAGE_AXIS));			
		centerCenterP.add(portIn);
		centerCenterP.add(Box.createVerticalGlue());

		//login
		loginLab = new JLabel("Login");
		loginIn = new JTextArea();		
		
		loginLab.setAlignmentX(Component.CENTER_ALIGNMENT);
		centerCenterP.setLayout(new BoxLayout(centerCenterP, BoxLayout.PAGE_AXIS));			
		centerCenterP.add(loginLab);
		
		loginIn.setAlignmentX(Component.CENTER_ALIGNMENT);
		centerCenterP.setLayout(new BoxLayout(centerCenterP, BoxLayout.PAGE_AXIS));			
		centerCenterP.add(loginIn);
		centerCenterP.add(Box.createVerticalGlue());
		
		//password
		passwordLab = new JLabel("Secret password");
		passwordIn = new JTextArea();		
		
		passwordLab.setAlignmentX(Component.CENTER_ALIGNMENT);
		centerCenterP.setLayout(new BoxLayout(centerCenterP, BoxLayout.PAGE_AXIS));			
		centerCenterP.add(passwordLab);
		
		passwordIn.setAlignmentX(Component.CENTER_ALIGNMENT);
		centerCenterP.setLayout(new BoxLayout(centerCenterP, BoxLayout.PAGE_AXIS));			
		centerCenterP.add(passwordIn);
		centerCenterP.add(Box.createVerticalGlue());
	
		
		//Puting
		center.add(centerCenterP);
		
		this.add(center, BorderLayout.CENTER);
		
		///end center Area
		
		
	}
	
	
	 @Override
	 public void paintComponent(Graphics g){
		g.drawImage(bg, 0, 0, this.getWidth(), this.getHeight(), this);
	 }
}
